#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct tree
{
	int n;
	tree * left;
	tree * right;
}ye;
typedef struct zhan
{
	ye * yezi;
	zhan * next;
}dian;
ye * init()
{
	ye * a=NULL;
	a=(ye *)malloc(sizeof(ye));
	a->n =0;
	a->left =NULL;
	a->right =NULL;
	return a;
}
void cre(ye * &t)
{
	int n;
	scanf("%d",&n);
	if(n==0)
	{
		t=NULL;
	}
	else
	{
		t=init();
		t->n=n;
		cre(t->left );
		cre(t->right );
	}
}
dian * initdian()
{
	dian * a=NULL;
	a=(dian *)malloc(sizeof(dian));
	a->yezi =NULL;
	a->next =NULL;
	return a;
}
dian * last(dian * p)
{
	dian * a=p;
	while(a->next !=NULL)
		a=a->next ;
	return a;
}
void jin(ye * t,dian * p)
{
	dian * a=last(p);
	dian * b=initdian();
	b->yezi =t;
	a->next =b;
}
void jinzhan(ye * t,dian * a)
{
	if(t==NULL)
	{
		jin(t,a);
	}
	else
	{
		jin(t,a);
		jinzhan(t->left ,a);
		jinzhan(t->right ,a);
	}
}
void putzhan(dian * a)
{
	a=a->next ;
	while(a!=NULL)
	{
		if(a->yezi !=NULL)
		{
			printf("%d\n",a->yezi ->n );
			a=a->next ;
		}
		else
		{
			printf("0\n");
			a=a->next ;
		}
	}
}
void main()
{
	ye * t=NULL;
	cre(t);
	dian * a=NULL;
	a=initdian();
	jinzhan(t,a);
	putzhan(a);
	getch();
}